package com.px.user.service;

import com.px.common.core.RespCode;
import com.px.common.core.Response;
import com.px.user.entity.ChatRobot;
import com.px.user.entity.IPBlack;
import com.px.user.entity.Room;
import com.px.user.mapper.ChatRobotMapper;
import com.px.user.mapper.IPBlackMapper;
import com.px.user.mapper.RoomMapper;
import com.px.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AdminService {

    @Autowired
    private IPBlackMapper ipBlackMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private ChatRobotMapper chatRobotMapper;

    public void iPBan(IPBlack ipBlack) {
        ipBlackMapper.insert(ipBlack);
        List<Long> userIds = userMapper.findByIp(ipBlack.getIp());
        if (userIds.isEmpty())
            return;
        userService.invokeTokens(userIds);
    }

    public void disableUser(){

    }

    public Response robotNumUpdate(ChatRobot chatRobot){
        if (chatRobot.getCategory().equals("robot-num")){
            Integer roomNum = roomMapper.pagingCount(Room.builder().build());
            Integer robotTotal = userMapper.robotCount();
            if (Integer.parseInt(chatRobot.getContent()) > robotTotal / roomNum)
                return Response.fail(RespCode.ROBOT_OVER, "单个房间机器人不能超过：" + robotTotal / roomNum);
        }
        chatRobotMapper.insert(chatRobot);
        return Response.success(null);
    }
}
